486B - OR in Matrix - CodeForces Solution


greedy hashing implementation *1300

Please click on ads to support us..

Python Code:

rows,columns=map(int,input().split()) ; mat=[] ; Rows_visited=set() ; soul=[] ; Columns_visited=set()
for row in range(rows):
    mat.append(list(map(int,input().split())))
    arr=[]
    for column in range(columns):
        arr.append("")
    soul.append(arr)

for row in range(rows):
    for column in range(columns):
        if mat[row][column]==0:
            if row not in Rows_visited:
                Rows_visited.add(row)
                for i in range(columns):
                    soul[row][i]=0
            if column not in Columns_visited:
                Columns_visited.add(column)
                for i in range(rows):
                    soul[i][column]=0
for row in range(rows):
    for column in range(columns):
        if soul[row][column]=="":
            soul[row][column]=1

cond=False
for row in range(rows):
    for column in range(columns):
        if mat[row][column]==0:
            if 1 in soul[row]:
                cond=True ; break
            for i in range(rows):
                if soul[i][column]==1:
                    cond=True ; break
        else:
            ones=False
            for i in range(rows):
                if soul[i][column]==1:
                    ones=True ; break
            if ones or 1 in soul[row]:
                pass
            else:
                cond=True; break
    if cond:print("NO") ; break
else:
    print("YES")
    for i in range(rows):
        print(*soul[i])

C++ Code:

#include <bits/stdc++.h>
#define ll                              long long
#define vr                              vector
#define pb                              push_back
#define vri                             vector <ll>
#define vrs                             vector <string>
#define take(arr,n)                     for(ll i=0; i<n; i++){ll a; cin>>a; arr[i]=a;}
#define takevrs(arr,n)                  for(ll i=0; i<n; i++){string s; cin>>s; arr[i]=s;}
#define showpair(v,n)                   for(ll i=0; i<n; i++){ cout<<v[i].first<<" "<<v[i].second<<" \n ";}

#define show(arr)                       for(auto i:arr) cout<<i<<" ";
#define sortv(v)                        sort(v.begin(),v.end());
#define mp                              make_pair
#define takepairii(v,n)                 for(ll i=0; i<n; i++){ ll x; ll y;cin>>x>>y; v.push_back(mp(x,y)); }
#define takepairss(v,n)                 for(ll i=0; i<n; i++){ string x; string y;cin>>x>>y; v.push_back(mp(x,y)); }
#define takepairis(v,n)                 for(ll i=0; i<n; i++){ ll x; string y;cin>>x>>y; v.push_back(mp(x,y)); }
 
using namespace std;

void solve(){
    
    int m,n;
    cin>>m>>n;
    int a[m][n];
    int b[m][n];
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            cin>>b[i][j];
            a[i][j]=0;
        }
    }
    int count=0,flag=0;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            if(b[i][j]==1){
                int check=0,r=0,c=0;
                flag=1;
                for(int k=0; k<m; k++){
                    if(b[k][j]==1) check++,r++;
                }
                for(int k=0; k<n; k++){
                    if(b[i][k]==1) check++,c++;
                }
                if(check==m+n){
                    count++;
                     a[i][j]=1;
                }
                else if(r<m && c<n){
                    cout<<"NO\n";
                    return;
                }
            }   
        }
    }
    if(!flag){
        cout<<"YES\n";
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                cout<<'0'<<" ";
            }       
            cout<<"\n";
        }
        return;
    }
    if(count>0){
       cout<<"YES\n";
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                cout<<a[i][j]<<" ";
            }       
            cout<<"\n";
        } 
    }
    else cout<<"NO\n";
    
}

int main() {
    
    solve();
    // int t;
    // cin>>t;
    // while(t--){
    //     solve();
    // }
}


Comments

Submit
0 Comments
More Questions

96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram